﻿using System.Windows;
using System.Windows.Input;

namespace K9Nano.Updater.Client.Mvvm;

public class RelayCommand<T> : ICommand
{
    /// <summary>
    /// 命令能否执行
    /// </summary>
    readonly Func<bool>? _canExecute;

    /// <summary>
    /// 命令执行的方法
    /// </summary>
    readonly Action<T> _execute;

    /// <summary>
    /// 命令的构造函数
    /// </summary>
    /// <param name="action">命令需执行的方法</param>
    /// <param name="canExecute">命令是否可以执行的方法</param>
    public RelayCommand(Action<T> action, Func<bool> canExecute)
    {
        _execute = action;
        _canExecute = canExecute;
    }

    public RelayCommand(Action<T> action)
    {
        _execute = action;
    }

    /// <summary>
    /// 判断命令是否可以执行
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public bool CanExecute(object? parameter)
    {
        if (_canExecute == null)
            return true;
        return _canExecute();
    }

    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="parameter"></param>
    public void Execute(object? parameter)
    {
        _execute((T)parameter!);
    }

    /// <summary>
    /// 事件追加、移除
    /// </summary>
    public event EventHandler? CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested += value;
        }
        remove
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested -= value;
        }
    }

}

public class RelayCommand : ICommand
{
    /// <summary>
    /// 命令能否执行
    /// </summary>
    readonly Func<bool>? _canExecute;

    /// <summary>
    /// 命令执行的方法
    /// </summary>
    readonly Action _execute;

    /// <summary>
    /// 命令的构造函数
    /// </summary>
    /// <param name="action">命令需执行的方法</param>
    /// <param name="canExecute">命令是否可以执行的方法</param>
    public RelayCommand(Action action, Func<bool> canExecute)
    {
        _execute = action;
        _canExecute = canExecute;
    }

    public RelayCommand(Action action)
    {
        _execute = action;
    }

    /// <summary>
    /// 判断命令是否可以执行
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public bool CanExecute(object? parameter)
    {
        if (_canExecute == null)
            return true;
        return _canExecute();
    }

    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="parameter"></param>
    public void Execute(object? parameter)
    {
        _execute();
    }

    /// <summary>
    /// 事件追加、移除
    /// </summary>
    public event EventHandler? CanExecuteChanged
    {
        add
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested += value;
        }
        remove
        {
            if (_canExecute != null)
                CommandManager.RequerySuggested -= value;
        }
    }
}